<template>
  <div class="cart">
      <header-bar :head-title="headTitle" goBack="true"></header-bar>
        <div class="list">
    <ul>
      <li class="item" v-for="(item, index) in cartsList" ref="temp" :key="index">
        <div class="item-container" id="getSize" ref="itemContainer">
          <div class="checkbox">
            <!-- <input type="checkbox" :id="index" :value="index" v-model="item.checked" style="visibility: hidden;"> -->
            <div  class="m-checkbox " :class="{checked: item.checked}" @click="check(item)"></div>
          </div>
          <div class="right-wrap">
            <div class="img-wrap">
              <img src="https://cdn.bestkeep.cn/goods/img/goods_1496976709841.jpg@160h_160w_2e" alt="">
            </div>
            <div class="desc-wrap">
              <p class="line1">黑凤梨 20寸PC膜拉链登机箱
              </p>
              <p class="line2">{{item.line2}}</p>
              <p class="line3">${{item.SalePrice}}</p>
            </div>
            <div class="total-num">

              <div class="con m-selNumRow">
                <div class="m-selnum">
                  <i class="less z-dis" @click="reduceNum(item)"></i>
                  <div class="textWrap"><input type="number" value="1" v-model="item.number" @blur="countChange(item)"></div>
                  <div class="more z-dis" @click="addNum(item)"></div>
                </div>
              </div>

            </div>
          </div>
        </div>
      </li>
    </ul>
  </div>
      <div class="cart-bottom">
        <div class="cart-label">
          
            <!-- <input type="checkbox" v-model="checked" style="visibility: hidden;"> -->
            <div  class="m-checkbox box" :class="{checked: allCheck}" @click="checkAll(!allCheck)"></div>
          <div class="money-count">
          合计：<span>￥{{totalMoney}}</span>  不含海关税和运费
          </div>
        </div>
        <router-link to="/order">结算</router-link>
      </div>
  </div>
</template>
<script>
import HeaderBar from '@/components/HeaderBar.vue'
import { Toast } from 'mint-ui'
export default {
  components: {
    HeaderBar
  },
  data () {
    return {
      allCheck: false,
      headTitle: '购物车',
      number: 1,
      totalMoney: 0,
      totalNum: 0,
      cartsList: [{
        li: 1,
        number: 1,
        kucun: 9,
        checked: false,
        SalePrice: 1
      }, {
        li: 2,
        number: 2,
        kucun: 2,
        checked: false,
        SalePrice: 1
      }, {
        li: 2,
        number: 3,
        kucun: 9,
        checked: false,
        SalePrice: 3
      }, {
        li: 4,
        number: 1,
        kucun: 55,
        checked: false,
        SalePrice: 1
      }]
    }
  },
  methods: {
    // 单选
    check (item) {
      var self = this
      item.checked = !item.checked
      let chkCount = 0;
      self.calcTotalMoney();
      this.cartsList.forEach(function (item) {
        if (item.checked) {
          chkCount++
        }
      })
      if (chkCount === this.cartsList.length) {
        self.allCheck = true;
      } else {
        self.allCheck = false;
      }
    },
    // 全选
    checkAll (isCheck) {
      this.allCheck = isCheck
      this.cartsList.forEach((item) => {
        if (typeof item.checked === 'undefined') {
          this.$set(item, 'checked', isCheck);
        } else {
          item.checked = isCheck;
        }
      })
      this.calcTotalMoney();
    },
    // 总金额计算
    calcTotalMoney () {
      let totalMoney = 0;
      let totalNum = 0;
      this.cartsList.forEach(function (item) {
        if (item.checked) {
          totalMoney += parseFloat(item.SalePrice) * parseInt(item.number);
          totalNum++
        }
      });
      this.totalMoney = totalMoney;
      this.totalNum = totalNum;
    },
    // 商品数量输入失去焦点判断
    countChange (item) {
      // var self = this
      if (isNaN(item.number)) {
        Toast('商品的数量不能为非数字');
        item.number = 1;
      }
      if (!item.number) {
        Toast('商品的数量不能为空');
        item.number = 1;
      }
      if (item.number <= 0) {
        Toast('商品的数量不能为0');
        item.number = 1;
      }
      if (item.number > item.kucun) {
        Toast('商品的数量不能超过库存');
        item.number = item.kucun;
      }
      this.calcTotalMoney();
    },
    // ++ 数量
    addNum (item) {
      var self = this
      console.log('add')
      if (item.number >= item.kucun) {
        Toast('超过最大数量')
        return false
      }
      item.number ++
      self.calcTotalMoney();
    },
    // -- 数量
    reduceNum (item) {
      var self = this
      if (item.number <= 1) {
        Toast('最少购买一件')
        return false
      }
      item.number --
      self.calcTotalMoney();
    }
  }
}
</script>
<style lang="scss">
.cart-bottom{
  
    width: 100%;
    height: 2rem;
    line-height: 2rem;
    color: #414141;
    font-size: .5rem;
    background-color: #fff;
    position: fixed;
    right: 0;
    left: 0;
    bottom: 0;
    margin: 0 auto;
    font-family: 'Microsoft YaHei';
    z-index: 9;
    display: flex; flex: 1;
    .cart-label{
      height: 2rem;
      text-align: left;
      display: flex;
      align-items: center;
      flex: 4;
      .box{margin: 0 .4rem;}
      .money-count{ display: flex; flex: 1;
      span{ margin-right:10px;}
      }
    }
    
    a{
      flex: 1;
      background: #f00;
      color:#fff;
      font-size: .6rem;
      font-weight: bold;
      height: 2rem;
      line-height: 2rem;
    }
}

.list{background: #fff;}
.item {
  height: 4rem;
  position: relative;
  overflow: hidden;
}

.item-container {
  display: flex;
  align-items: center;
  height: 100%;
  transition: all .2s ease-in;
}

.right-wrap {
  display: flex;
  align-items: center;
  flex: 1;
}

.desc-wrap {
  flex: 1;
}

.line1 {
  line-height: .42667rem;
  font-size: .37333rem;
  margin-bottom: .13333rem;
}

.line2 {
  font-size: .32rem;
  line-height: .37333rem;
  margin-bottom: .10667rem;
  color: #666;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.line3 {
  font-size: .37333rem;
  line-height: .42667rem;
}

.checkbox {
  padding: 0 .2846rem;
}

.img-wrap {
  margin-right: .26667rem;
}

.img-wrap img {
  width: 2.5rem;
  height: 2.5rem;
}

.total-num {
  align-items: flex-start;
  font-size: 16px;
  margin-right: 8px;
}


/*del*/

.del-box {
  position: absolute;
  top: 0;
  left: 100%;
  width: 1.86667rem;
  background-color: #e83a3a;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.u-icon-trash {
  display: block;
  width: .54667rem;
  height: .56rem;
  background-size: cover;
}


/*- + 不可点击*/
.m-selnum.dlr {
    background-position: 0 -2.08rem;
}
/*+ 可以点击*/
.m-selnum.dl {
    background-position: 0 -1.04rem;
}
/*- + 都可以点击*/
.m-selnum {
    background-position: 0 -8.29333rem;
    width: 4.13333rem;
    height: .93333rem;
}
.m-selnum.allactive{
    background-position: 0 -8.29333rem;  
}

.m-selnum, .m-selnum-1 {
    display: inline-block;
    background-image: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/selnum-s2aa5e4fbe0-14d469f19a.png);
    background-repeat: no-repeat;
    background-size: 4.12rem 9.2rem;
    vertical-align: middle;
}
.m-selnum .less, .m-selnum .more {
    width: 1.2rem;
}
.m-selnum .less, .m-selnum .more, .m-selnum .textWrap {
    position: relative;
    float: left;
    height: .88rem;
}
.m-selnum .less, .m-selnum .more, .m-selnum .textWrap, .m-selnum input {
    border: none;
    background-color: transparent;
}
.m-selnum .textWrap {
    position: relative;
    width: 1.72rem;
    text-align: center;
    font-size: .32rem;
    line-height: .88rem;
}
.m-selnum .less, .m-selnum .more, .m-selnum .textWrap {
    position: relative;
    float: left;
    height: .88rem;
}
.m-selnum .less, .m-selnum .more, .m-selnum .textWrap, .m-selnum input {
    border: none;
    background-color: transparent;
}
.m-selnum .textWrap input {
    position: relative;
    z-index: 1;
    font-size: .32rem;
}
.m-selnum input {
    height: 100%;
    width: 100%;
    box-sizing: border-box;
    margin: 0;
    text-align: center;
    color: #333;
}
.m-selnum .less, .m-selnum .more {
    width: 1.2rem;
}
</style>

